package com.htjc.integral.house.controller;

import java.sql.Timestamp;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.htjc.integral.common.annotation.ExecuteTime;
import com.htjc.integral.house.service.IConsService;
import com.htjc.integral.house.util.JsonUtil;
import com.htjc.integral.house.util.ResultModel;
import com.htjc.integral.house.util.StringUtils;
import com.htjc.integral.house.util.enums.AccountReturn;

import lombok.extern.slf4j.Slf4j;

/**
 * 户号服务
 * @author zhangjiajia
 */
@Slf4j(topic = "jfLog")
@RestController
@RequestMapping(value = "/house")
public class ConsController {

	@Autowired
	IConsService consService;
	
	/**
	 * 
	 * @param jsonObject POWER_USER_NO 户号
	 * 					 PROVINCE_CODE 网省编码-5位国标码
	 * 					 ORG_NO 供电单位
	 * @return
	 */
	@ExecuteTime
	@RequestMapping(value = {"/queryConsBalance"},produces = {"text/json;charset=UTF-8;", "application/json;"})
	public String queryConsBalance( @RequestBody String params) {
		String result = null;
		try {
			System.out.println(params);
			isNull(params);
			
			//处理特殊字符的编码问题 
			JSONObject jsonObject = JSONObject.parseObject(params);
			log.info(">>>>>>>>===查询户号余额开始===<<<<<<<<,参数={}", jsonObject);
			String powerUserNo = jsonObject.getString("POWER_USER_NO");
			String provinceCode = jsonObject.getString("PROVINCE_CODE");
			String orgNo = jsonObject.getString("ORG_NO");
			
		
			log.info(">>>>>>>>===查询户号余额===<<<<<<<<-用电户号={},网省编码={},供电单位={}", powerUserNo, provinceCode, orgNo);
			result = consService.queryConsBalance(provinceCode, orgNo, powerUserNo).toString();
			log.info(">>>>>>>>===查询户号余额结束===<<<<<<<<-用电户号={},网省编码={},供电单位={},返回值={}", powerUserNo, provinceCode, orgNo, result);
		} catch (Exception e) {
			log.error(">>>>>>>>===查询户号余额异常===<<<<<<<<-错误信息={}", e.toString());
			return JSONObject.parseObject(JsonUtil.bean2json(ResultModel.AccountReturnError(AccountReturn.ACCOUNT_ERROR))).toString();
		}
		return result;
	}

	/**
	 * 户号余额增加
	 * @param jsonObject PROVINCE_CODE 网省编码-5位国标码
	 * 					 ORG_NO 供电单位
	 * 					 POWER_USER_NO 户号
	 * 					 ACCOUNT_MAIN_ID 业务ID
	 * 					 INCOME_SOURCE 收入来源
	 * 					 INCOME_TYPE 收入类型
	 * 					 OPERATE_TYPE 操作类型
	 * 					 INCOME_NUM 收入总金额
	 * 					 STATE_TIME 积分生成时间
	 * @return
	 */
	@RequestMapping(value = {"/addConsBalance"},produces = {"text/json;charset=UTF-8;", "application/json;"})
	public String addConsBalance(@RequestBody  String params) {
		String result = null;
		Map<String, Object> resultMap = new HashMap<String, Object>();
		try {
			isNull(params);
			
			JSONObject jsonObject = JSONObject.parseObject(params);
			log.info(">>>>>>>>===户号余额增加开始===<<<<<<<<,参数={}", jsonObject.toString());
			//校验参数
			if (!checkParamer(jsonObject)) {
				log.info(">>>>>>>>===户号余额增加失败===<<<<<<<<,部分参数为空");
				return JSONObject.parseObject(JsonUtil.bean2json(ResultModel.AccountReturnError(AccountReturn.ACCOUNT_ERROR_NOPARAMS))).toString();
			} 
			resultMap.put("POWER_USER_NO", jsonObject.get("POWER_USER_NO"));
			resultMap.put("ACCOUNT_MAIN_ID", jsonObject.get("ACCOUNT_MAIN_ID"));
			resultMap.put("INCOME_NUM", new Timestamp(System.currentTimeMillis()));
		
			log.info(">>>>>>>>===户号余额增加落库中===<<<<<<<<");
			String accountId = consService.addConsBalance(jsonObject).toString();
			
			resultMap.put("STATUS", "02");
			if ("-1".equals(accountId)) {
				resultMap.put("STATUS", "04");
				resultMap.put("REMARK", "未知的业务类型");
			} 
			log.info(">>>>>>>>===户号余额增加落库完成===<<<<<<<<");
			result = JSONObject.parseObject(JsonUtil.bean2json(ResultModel.AccountReturnSucceed(resultMap))).toString();
		} catch (Exception e) {
			log.error(">>>>>>>>===户号余额增加异常===<<<<<<<<,错误信息={}", e.toString());
			resultMap.put("STATUS", "03");
			return JSONObject.parseObject(JsonUtil.bean2json(new ResultModel(AccountReturn.ACCOUNT_ERROR, resultMap))).toString();
		} 
		return result;
	}
	
	/**
	 * 户号余额扣减
	 * @param jsonObject PROVINCE_CODE 网省编码-5位国标码
	 * 					 ORG_NO 供电单位
	 * 					 POWER_USER_NO 户号
	 * 					 ACCOUNT_MAIN_ID 业务ID
	 * 					 INCOME_SOURCE 收入来源
	 * 					 OPERATE_TYPE 操作类型
	 * 					 INCOME_NUM 收入总金额
	 * @return
	 */
	@RequestMapping(value = {"/dedConsBalance"}, produces = {"text/json;charset=UTF-8;", "application/json;"})
	public String dedConsBalance(@RequestBody String params) {
		String result = null;
		JSONObject jsonObject = null;
		Map<String, Object> resultMap = new HashMap<String, Object>();
		try {
			System.err.println("++++++++++++++"+params);
			isNull(params);
			jsonObject = JSONObject.parseObject(params);
			jsonObject.put("STATE_TIME", new Timestamp(System.currentTimeMillis()));
			//检验参数
			if (!checkParamer(jsonObject)) {
				log.info(">>>>>>>>===户号余额扣减失败===<<<<<<<<,部分参数为空");
				return JSONObject.parseObject(JsonUtil.bean2json(ResultModel.AccountReturnError(AccountReturn.ACCOUNT_ERROR_NOPARAMS))).toString();
			}
			
			resultMap.put("POWER_USER_NO", jsonObject.getString("POWER_USER_NO"));
			resultMap.put("ACCOUNT_MAIN_ID", jsonObject.getString("ACCOUNT_MAIN_ID"));
			resultMap.put("INCOME_TIME", new Timestamp(System.currentTimeMillis()));
		
			log.info(">>>>>>>>===户号余额扣减落库中===<<<<<<<<");
			JSONObject json = consService.dedConsBalance(jsonObject);
			log.info(">>>>>>>>===户号余额扣减落库完成===<<<<<<<<");
			
			if (json == null) {
				resultMap.put("STATUS", "03");
				resultMap.put("INCOME_NUM", jsonObject.getBigDecimal("INCOME_NUM"));
				resultMap.put("REMARK", "该户不存在或者该户余额不足");
				return JSONObject.parseObject(JsonUtil.bean2json(new ResultModel(AccountReturn.ACCOUNT_ERROR, resultMap))).toString();
			} else {
				resultMap.put("STATUS", "01");
				resultMap.put("ACCOUNT_DED_ID", json.getString("ACCOUNT_DED_ID"));
				resultMap.put("INCOME_NUM", json.getBigDecimal("INCOME_NUM"));
			}
			result = JSONObject.parseObject(JsonUtil.bean2json(ResultModel.AccountReturnSucceed(resultMap))).toString();
		} catch (Exception e) {
			log.error(">>>>>>>>===户号余额扣减异常===<<<<<<<<,错误信息={}", e.toString());
			resultMap.put("STATUS", "03");
			resultMap.put("INCOME_NUM", jsonObject.getBigDecimal("INCOME_NUM"));
			return JSONObject.parseObject(JsonUtil.bean2json(new ResultModel(AccountReturn.ACCOUNT_ERROR, resultMap))).toString();
		}
		return result;
	}
	
	public boolean checkParamer(JSONObject jsonObject) {
		String provinceCode = jsonObject.getString("PROVINCE_CODE");
		String consNo = jsonObject.getString("POWER_USER_NO");
		String accountId = jsonObject.getString("ACCOUNT_MAIN_ID");
		String incomeType = jsonObject.getString("INCOME_TYPE");
		String incomeNum = jsonObject.getString("INCOME_NUM");
		String stateTime = jsonObject.getString("STATE_TIME");
		
		if (StringUtils.isNull(provinceCode, consNo, accountId, incomeNum, incomeType, stateTime)){
			return false;
		}
		return true;
	}
	
	private void isNull(String str) {
		if (StringUtils.isNull(str)) {
			throw new RuntimeException("参数为空");
		}
	}
}




























